//
//  GPACalcViewController.m
//  GPA Calculator
//
//  Created by iD Student on 6/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GPACalcViewController.h"

@interface GPACalcViewController ()

@end

@implementation GPACalcViewController
@synthesize subj1 = _subj1;
@synthesize subj2 = _subj2;
@synthesize subj3 = _subj3;
@synthesize subj4 = _subj4;
@synthesize subj5 = _subj5;
@synthesize outlbl = _outlbl;

- (void)viewDidLoad {
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload {
    [self setSubj1:nil];
    [self setSubj2:nil];
    [self setSubj3:nil];
    [self setSubj4:nil];
    [self setSubj5:nil];
    [self setOutlbl:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)calc:(id)sender {
    float avg = [self getGpaNumFromPct: [self.subj1.text floatValue]];
    avg += [self getGpaNumFromPct: [self.subj2.text floatValue]];
    avg += [self getGpaNumFromPct: [self.subj3.text floatValue]];
    avg += [self getGpaNumFromPct: [self.subj4.text floatValue]];
    avg += [self getGpaNumFromPct: [self.subj5.text floatValue]];
    
    avg /= 5;
        
    self.outlbl.text = [self convertToString: avg];
}

- (float) getGpaNumFromPct:(int) pct {
    if (pct < 60) {
        return 0;
    } else if (pct < 63) {
        return .67;
    } else if (pct < 67) {
        return 1;
    } else if (pct < 70) {
        return 1.33;
    } else if (pct < 73) {
        return 1.67;
    } else if (pct < 77) {
        return 2;
    } else if (pct < 80) {
        return 2.33;
    } else if (pct < 83) {
        return 2.66;
    } else if (pct < 87) {
        return 3;
    } else if (pct < 90) {
        return 3.33;
    } else if (pct < 93) {
        return 3.67;
    } else if (pct < 97) {
        return 4;
    } else {
        return 4.33;
    }
    return 0;
}

-(NSString *) convertToString:(float) inNum {
    return [NSString stringWithFormat: @"%s", inNum];
}
@end
